﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Bll;

namespace Runda.EShop.Web.Message
{
    public partial class MessageList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;

            parm = new SqlDbParameter();
            parm.ColumnName = "[ObjectType]";
            parm.ColumnType = DbType.Int32;
            parm.ParameterName = "@ObjectType";
            parm.ParameterValue = (int)EnumHelper.UserType.Customer;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            parm = new SqlDbParameter();
            parm.ColumnName = "[ObjectID]";
            parm.ColumnType = DbType.Guid;
            parm.ParameterName = "@ObjectID";
            parm.ParameterValue = base.User.Identity.LoginUser.UserID;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            int RecordCount = 0;
            DataTable list = new SysMessageBll().GetUserMessage(parms, " CreatedAt DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvMessageList.DataSource = list;
            gvMessageList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void gvMessageList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblIsRead = e.Row.FindControl("lblIsRead") as Label;
                DataRowView dv = e.Row.DataItem as DataRowView;

                lblIsRead.Text = Convert.ToInt32(dv["IsRead"].ToString()) == 0 ? "未读" : "已读";
            }
        }
    }
}